//通过taskId获取日志
//config为参数及回调方法
//可选参数
//taskId
//hostName
//targetId: 用来显示log的document id
//complete: 方法,最后调用
//error:    方法,出错时调用
//render:   显示返回日志时调用,可选,默认直接在targetId元素上显示

var count = {};
window.logMaxLen = 100 * 1024;
window.logPeriod = 500;
function showLogByTaskId(config){//documentCallback, requestCallback){


    var offset = config.offset || -1
    if(typeof config.reqNum == "undefined")
        config.reqNum = 0;
    $.ajax({
        url: "/test/taskLog.html",
        data: {taskId: config.taskId, host: config.hostName, offset: offset},
        dataType: "json",
        success: function(r){


                    config.reqNum = config.reqNum + 1;

                    if(config.render == null || typeof config.render == "undefined"){

                        if(r[0].message != ""){
                            if(offset < 0 )
                                $("#" + config.targetId).html(r[0].message)
                            else{
                                var obj = $("#" + config.targetId);
                                var html = obj.html();
                                if(html.length > logMaxLen){
                                    obj.html(html.substring(logMaxLen / 2 , html.length) + r[0].message)
                                }else{
                                    obj.html(html + r[0].message)
                                }
                            }
                        }

                    }else{
                        config.render(r[0], config)
                    }


            //if(config.requestCallback != null && typeof config.requestCallback != "undefined") {
            //
            //    config.requestCallback(r)
            //}

            if(!r[0].status.finished && stop == false){

                var c =(count[r[0].hostName] | 0 ) + 1;
                count[r[0].hostName] = c;
                if(config.targetId != null && typeof config.targetId != "undefined"){

                    config.id = c;
                    if(c % 2 == 0){
                        $("#" + config.targetId).css("border", "1px solid white")
                    }else{
                        $("#" + config.targetId).css("border", "1px solid #ccc")

                    }
                }
                config.offset = r[0].offset;

                setTimeout(function(){showLogByTaskId(config)}, logPeriod)
            }else{



                if(r[0].status.isError && config.error != null && typeof config.error != "undefined") {

                    config.error(config)

                    if(config.targetId != null && typeof config.targetId != "undefined") {

                        $("#" + config.targetId).css("border", "1px solid red")
                    }
                }else{

                    if(config.complete != null && typeof config.complete != "undefined") {

                        config.complete(config)

                    }

                    if(config.targetId != null && typeof config.targetId != "undefined") {

                        $("#" + config.targetId).css("border", "1px solid green")
                    }
                }


            }

        }

    })
}


function tail(config){//documentCallback, requestCallback){


    var offset = config.offset || -1
    //data: {appName: appName, host: host, path: path, type: "json", offset: offset || -1, size: 4000},
    if(config.before != null && typeof config.before != "undefined")
        if(config.before(config) == false) return;

    if(typeof config.reqNum == "undefined")
        config.reqNum = 0;
    $.ajax({
        url: "/appManager/appManagerLogDetail.html",
        data: {appName: config.appName, host: config.host, offset: offset, type: "json",path: config.path},
        dataType: "json",
        success: function(r){


                config.reqNum = config.reqNum + 1;


                if(config.render == null || typeof config.render == "undefined"){

                    if(r[0].message != ""){
                        var html = editor.doc.getValue();
                        if(html.length > logMaxLen){
                            html = html.substring(logMaxLen / 2, html.length) + r[0].message
                        }else{
                            html = html + r[0].message
                        }

                        var top =  autoScroll()
                        editor.doc.setValue(html)
                        editor.scrollTo(-1, top)
                    }

                }else{

                    config.render(r[0], config)
                }


            var offset = r[0].offset;
            if(offset <= 0){
                if(config.lastOffset > 0)offset = config.lastOffset;
            }
            else{
                config.lastOffset = offset;
            }
            config.offset = offset

            if(stop == false){
                if(config.success != null && typeof config.success != "undefined")
                    config.success(config);
                setTimeout(function(){tail(config)}, logPeriod)

            }


        }

    })
}


//调整日志框高度
function reSizeHeight(id){

    var height = window.screen.availHeight

    if(typeof id == "undefined") id = "moduleTable";
    var obj = $("#" + id)

    if(obj.size() > 0){
        obj = obj[0]

        height = height - (obj.clientHeight + obj.offsetTop + 150)
    }
    editor.setSize(-1,height)

}
